/*
 * @lc app=leetcode.cn id=3494 lang=cpp
 * @lcpr version=30204
 *
 * [3494] 酿造药水需要的最少总时间
 */


// @lcpr-template-start
using namespace std;
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// @lcpr-template-end
// @lc code=start
class Solution {
public:
    long long minTime(vector<int>& skill, vector<int>& mana) {
        int n = skill.size(), m = mana.size();
        vector<long long> time(n + 1, 0);
        for (int i = 0; i < m ; ++i) {

            time[0] = time[1];
            for (int j = 1; j < n ; ++j) {
                time[j] = max(time[j + 1], time[j - 1] + mana[i] * skill[j - 1]);
            }
            time[n] = time[n - 1] + mana[i] *skill[n - 1];

            for (int j = n - 1; j >= 0 ; --j) {
                time[j] = time[j + 1] - mana[i] * skill[j];
            }
        }
        return time[n];
    }
};
// @lc code=end



/*
// @lcpr case=start
// [1,5,2,4]\n[5,1,4,2]\n
// @lcpr case=end

// @lcpr case=start
// [1,1,1]\n[1,1,1]\n
// @lcpr case=end

// @lcpr case=start
// [1,2,3,4]\n[1,2]\n
// @lcpr case=end

 */

